<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>组织架构 - TMS</title>
    <link rel="stylesheet" href="/view/frame/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/view/frame/static/css/global.css?v=1.0">
    <link rel="stylesheet" href="/view/tms/static/css/iframe.css?v=1.0">
    <style>
        .organize {
            padding: 10px 15px 10px 0;
            overflow: auto;
        }
        
        #menus cite p {
            margin: 0;
            padding: 0;
            display: inline;
        }
        
        #menus cite p.current {
            color: #f60 !important;
        }
        
        #menus cite p span.manager,
        #menus cite p.current span.manager {
            color: #777;
        }
        
        #menus cite p.cancel {
            color: #999;
        }
        
        #menus cite p.cancel span.manager {
            color: #999;
        }
        
        #menus cite p.cancel span.status {
            color: #999;
        }
        
        .left-panel {
            padding: 0;
        }
        
        .right-panel {
            padding: 8px 15px 0;
            border-left: #ddd 1px solid;
            overflow: auto;
        }
    </style>
</head>

<body style="margin:0 0 0 15px;">
    <div class="layui-row">
        <div class="layui-col-lg3 layui-col-md3 layui-col-sm4 left-panel">
            <!--start: 按钮组-->
            <div class="panel-handle btns-bar" style="border-bottom:#ddd 1px solid; padding-bottom:10px;">
                <div class="layui-btn-group fl">
                    <button class="layui-btn layui-btn-normal layui-btn-small" data-type="addCompany">新增公司</button>
                    <button class="layui-btn layui-btn-normal layui-btn-small" data-type="addDepartment">新增部门</button>
                    <button class="layui-btn layui-btn-normal layui-btn-small" data-type="statusCancle">停用</button>
                    <button class="layui-btn layui-btn-normal layui-btn-small" data-type="statusStart">启用</button>
                </div>
                <div class="clr"></div>
            </div>
            <!--end: 按钮组-->

            <div class="organize">
                <ul id="menus"></ul>
            </div>
        </div>
        <div class="layui-col-lg9 layui-col-md9 layui-col-sm8 right-panel">
            <div id="rightMainer"></div>
        </div>
    </div>
</body>
<script charset="utf-8" src="/view/frame/static/js/jquery.min.js?v=1.0"></script>
<script charset="utf-8" src="/view/frame/layui/layui.js"></script>
<script charset="utf-8" src="/view/tms/static/js/tms_index.js?v=1219"></script>
<script charset="utf-8" src="/view/frame/static/js/lang_zh_CN.js?v=1.0"></script>
<script charset="utf-8" src="/view/frame/static/js/layui.district_1.js?v=1.0"></script>
<script charset="utf-8" src="/view/tms/static/js/tms_company.js?v=1.0"></script>
<!-- 工具库依赖 -->
<script charset="utf-8" src="/view/frame/static/js/errorCodeMap.js"></script>
<script charset="utf-8" src="/view/frame/static/js/HC.js?v=1.4"></script>
<script charset="utf-8" src="/view/tms/static/js/validator.js"></script>
<script charset="utf-8" src="/view/tms/static/js/bizUtil.js"></script>
<script>
    $(function() {
        autoHeight();
        $(window).on('resize', function() {
            autoHeight();
        });

        //组织架构右边内容高度
        function autoHeight() {
            var $winHeight = $(window.parent.document).height() - 110 - 39 - 8;
            $('.organize').height($winHeight - 63);
            $('.right-panel').height($winHeight);
        }

        $('#rightMainer').load('addCompany.html', function() {
            HCValidator.addVerifyToContainer($('form'));
        });
    });

    layui.use(['form', 'layer', 'tree'], function() {
        var form = layui.form,
            layer = layui.layer,
            $ = layui.jquery;

        //获取组织架构
        var setMenus = function() {
            $.get('/ucenter/centre/core/organization/all.shtml', function(d) {
                var $code = d.code,
                    $msg = d.msg,
                    $objects = d.objects;

                if ($code != 'SUCCESS') return false;

                //重新编绎json
                var $allArr = setTreeData(0, $objects);
                //console.log($allArr);

                //参数设置
                var $options = {
                        elem: '#menus',
                        //check: 'checkbox',
                        drag: true,
                        //checkboxName: 'mids',
                        click: function(item) {
                            $('#menus cite p').removeClass('current');
                            $('#cc' + item.rid + ' p').addClass('current');
                            $('#menus').data('id', item.rid);
                            $('#menus').data('type', item.type);
                            if (item.status == 3) {
                                setLayerAlert(parent.layer, '该组织架构已停用，请启动后查看', {
                                    btn: ['立即启用', '取消'],
                                    yes: function(index) {
                                        if (item.type == 1) {
                                            roleStatus('/ucenter/centre/permi/company/status/' + item.rid + '.shtml', item.rid, 1);
                                        } else if (item.type == 2) {
                                            roleStatus('/ucenter/centre/permi/department/status/' + item.rid + '.shtml', item.rid, 1);
                                        }
                                        parent.layer.close(index);
                                    }
                                });
                            } else {
                                if (item.type == 1) {
                                    $('#rightMainer').load('editCompany.html', function() {
                                        HCValidator.addVerifyToContainer($('form'));
                                    });
                                } else if (item.type == 2) {
                                    $('#rightMainer').load('editDepartment.html', function() {
                                        HCValidator.addVerifyToContainer($('form'));
                                    });
                                }
                            }
                        },
                        nodes: $allArr
                    }
                    //树形菜单
                layui.tree($options);
            }, 'json');
        }

        setMenus();

        var active = {
            //新增公司
            addCompany: function() {
                $('#menus cite p').removeClass('current');
                $('#rightMainer').load('addCompany.html', function() {
                    HCValidator.addVerifyToContainer($('form'));
                });
            },
            //新增部门
            addDepartment: function() {
                $('#menus cite p').removeClass('current');
                $('#rightMainer').load('addDepartment.html', function() {
                    HCValidator.addVerifyToContainer($('form'));
                });
            },
            //停用
            statusCancle: function() {
                var $getId = $('#menus').data('id') || 0,
                    $getType = $('#menus').data('type') || 0;
                if ($getId == 0) {
                    setLayerAlert(parent.layer, '请选择需停用的公司或部门');
                    return false;
                } else {
                    //停用后，将（公司/部门）的（子级公司/部门）将往上提一级，确定停用吗？
                    setLayerConfirm(parent.layer, '确定停用吗？', function(index) {
                        //接口
                        if ($getType == 1) {
                            roleStatus('/ucenter/centre/permi/company/status/' + $getId + '.shtml', $getId, 3);
                        } else if ($getType == 2) {
                            roleStatus('/ucenter/centre/permi/department/status/' + $getId + '.shtml', $getId, 3);
                        }
                        parent.layer.close(index);
                    });
                }
            },
            //启用
            statusStart: function() {
                var $getId = $('#menus').data('id') || 0,
                    $getType = $('#menus').data('type') || 0;
                if ($getId == 0) {
                    setLayerAlert(parent.layer, '请选择需启用的公司或部门');
                    return false;
                } else {
                    setLayerConfirm(parent.layer, '确定启用吗？', function(index) {
                        //接口
                        if ($getType == 1) {
                            roleStatus('/ucenter/centre/permi/company/status/' + $getId + '.shtml', $getId, 1);
                        } else if ($getType == 2) {
                            roleStatus('/ucenter/centre/permi/department/status/' + $getId + '.shtml', $getId, 1);
                        }
                        parent.layer.close(index);
                    });
                }
            }
        };

        $('.btns-bar .layui-btn').on('click', function() {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //改变状态，3停用，1启用，2删除
        function roleStatus($url, $id, $status) {
            $.ajax({
                type: 'PUT',
                url: $url + '?status=' + $status,
                success: function(d) {
                    var $code = d.code,
                        $msg = d.msg,
                        $objects = d.objects;

                    if ($code === 'SUCCESS') {
                        window.location.reload();
                    } else {
                        setLayerAlert(parent.layer, '状态改变失败');
                    }
                }
            });
        }
    });
</script>

</html>